package com.parroquia.sacramento.action;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.parroquia.common.util.Utilitario;
import com.parroquia.sacramento.model.Bautismo;
import com.parroquia.sacramento.model.Confirmacion;
import com.parroquia.sacramento.model.Matrimonio;
import com.parroquia.sacramento.model.PrimeraComunion;
import com.parroquia.sacramento.model.Uncion;


public class Sacramento extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	
	private Bautismo bautismo;
	private PrimeraComunion comunion;
	private Confirmacion confirmacion;
	private Matrimonio matrimonio;
	private Uncion uncion;
	private String message;
	private String opc;
	
	
	@Action(value="register-sacramento", 
			results={
				@Result(name="exito", location="/message.jsp"),
				@Result(name="error", location="/index_sacramento.jsp")
				}
	)
	
	@Validations(
		       requiredStrings =
		        	{  @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "name", key = "course.name.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "apePat", key = "course.last_name.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "apeMat", key = "course.username.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "dni", key = "course.password.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "nombrePad", key = "course.confirmPassword.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "apePatPad", key = "course.mail.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "apeMatPad", key = "course.phone.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "dniPad", key = "course.course.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "nombreMad", key = "course.course.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "apePatMad", key = "course.course.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "apeMatMad", key = "course.course.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "dniMad", key = "course.course.required"),   		   
		        	},
		        //Regla de Negocio
		        emails = 
		    		{ 
		    		   @EmailValidator(type = ValidatorType.SIMPLE, fieldName = "course.mail", key = "course.mail.good")
		    		}, 
		    	stringLengthFields =
          	{
		    		   @StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim = true, minLength="3" , maxLength = "8", fieldName = "course.password", key = "course.password.length"),
		    		   @StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim = true, minLength="3" , maxLength = "8", fieldName = "course.confirmPassword", key = "course.confirmPassword.length"),
		    		   @StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim = true, minLength="9" , maxLength = "9", fieldName = "course.phone", key = "course.phone.length")
          	},
          	intRangeFields = 
         		{ 
		    		   @IntRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "course.age", min = "18", max = "40", key="course.age.range")
         		}
		    )
	
	public String execute(){
		String result="exito";
		if(opc.equals("bautismo")){
			EntityManager em=
					Utilitario.getInstance().getEntityManager();
			EntityTransaction tx=em.getTransaction();
			try{
				tx.begin();
				em.persist(bautismo);
				tx.commit();
				result="exito";
				message = getText("bautismo.message.exito");
			}catch(Exception ex){
				ex.printStackTrace();
				tx.rollback();
				message = getText("bautismo.message.error");
			}
		}else if(opc.equals("pcomunion")){
			EntityManager em=
					Utilitario.getInstance().getEntityManager();
			EntityTransaction tx=em.getTransaction();
			try{
				tx.begin();
				em.persist(comunion);
				tx.commit();
				result="exito";
				message = getText("comunion.message.exito");
			}catch(Exception ex){
				ex.printStackTrace();
				tx.rollback();
				message = getText("comunion.message.error");
			}
		}else if(opc.equals("confirmacion")){
			
			EntityManager em=
					Utilitario.getInstance().getEntityManager();
			EntityTransaction tx=em.getTransaction();
			try{
				tx.begin();
				em.persist(confirmacion);
				tx.commit();
				result="exito";
				message = getText("confirmacion.message.exito");
			}catch(Exception ex){
				ex.printStackTrace();
				tx.rollback();
				message = getText("confirmacion.message.error");
			}
			
		}else if(opc.equals("matri")){
			
			EntityManager em=
					Utilitario.getInstance().getEntityManager();
			EntityTransaction tx=em.getTransaction();
			try{
				tx.begin();
				em.persist(matrimonio);
				tx.commit();
				result="exito";
				message = getText("matrimonio.message.exito");
			}catch(Exception ex){
				ex.printStackTrace();
				tx.rollback();
				message = getText("matrimonio.message.error");
			}
			
			}else{
			
				EntityManager em=
						Utilitario.getInstance().getEntityManager();
				EntityTransaction tx=em.getTransaction();
				try{
					tx.begin();
					em.persist(uncion);
					tx.commit();
					result="exito";
					message = getText("uncion.message.exito");
				}catch(Exception ex){
					ex.printStackTrace();
					tx.rollback();
					message = getText("uncion.message.error");
				}
		}
		return result;
	}
}
